package com.ruoyi.outcome.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.outcome.domain.vo.ResearchAwardTrack;
import com.ruoyi.outcome.domain.vo.ResearchPaperTrack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.outcome.domain.ResearchPaper;
import com.ruoyi.outcome.service.IResearchPaperService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 论文管理Controller
 * 
 * @author zxh
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/paper")
public class ResearchPaperController extends BaseController
{
    @Autowired
    private IResearchPaperService researchPaperService;

    /**
     * 查询论文管理列表
     */
    @RequiresPermissions("outcome:paper:list")
    @GetMapping("/list")
    public TableDataInfo list(ResearchPaper researchPaper)
    {
        startPage();
        researchPaper.setIsDeleted(0);
        List<ResearchPaper> list = researchPaperService.selectResearchPaperList(researchPaper);
        return getDataTable(list);
    }

    /**
     * 导出论文管理列表
     */
    @RequiresPermissions("outcome:paper:export")
    @Log(title = "论文管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchPaper researchPaper)
    {
        List<ResearchPaper> list = researchPaperService.selectResearchPaperList(researchPaper);
        ExcelUtil<ResearchPaper> util = new ExcelUtil<ResearchPaper>(ResearchPaper.class);
        util.exportExcel(response, list, "论文管理数据");
    }

    /**
     * 获取论文管理详细信息
     */
    @RequiresPermissions("outcome:paper:query")
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId)
    {
        return success(researchPaperService.selectResearchPaperByPaperId(paperId));
    }

    /**
     * 新增论文管理
     */
    @RequiresPermissions("outcome:paper:add")
    @Log(title = "论文管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchPaper researchPaper)
    {

        researchPaper.setCreatedBy(SecurityUtils.getUsername());

        ResearchPaperTrack paperTrack= new ResearchPaperTrack();
        BeanUtils.copyProperties(researchPaper, paperTrack);
        paperTrack.setActionType("ADD");
        researchPaperService.insertResearchPaperTrack(paperTrack);
        return toAjax(researchPaperService.insertResearchPaper(researchPaper));
    }

    /**
     * 修改论文管理
     */
    @RequiresPermissions("outcome:paper:edit")
    @Log(title = "论文管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchPaper researchPaper)
    {
        researchPaper.setUpdatedBy(SecurityUtils.getUsername());
        researchPaper.setUpdatedAt(new Date());

        ResearchPaperTrack paperTrack= new ResearchPaperTrack();
        BeanUtils.copyProperties(researchPaper, paperTrack);
        paperTrack.setActionType("UPDATE");
        researchPaperService.insertResearchPaperTrack(paperTrack);
        return toAjax(researchPaperService.updateResearchPaper(researchPaper));
    }

    /**
     * 删除论文管理
     */
    @RequiresPermissions("outcome:paper:remove")
    @Log(title = "论文管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds)
    {
        for(Long paperId:paperIds)
        {
            ResearchPaper researchPaper = researchPaperService.selectResearchPaperByPaperId(paperId);
            researchPaper.setIsDeleted(1);
            researchPaper.setDeletedBy(SecurityUtils.getUsername());
            researchPaper.setDeletedAt(new Date());
            researchPaperService.updateResearchPaper(researchPaper);
            ResearchPaperTrack paperTrack= new ResearchPaperTrack();
            BeanUtils.copyProperties(researchPaper, paperTrack);
            paperTrack.setActionType("DELETE");
            researchPaperService.insertResearchPaperTrack(paperTrack);
        }
        return success();
//        return toAjax(researchPaperService.deleteResearchPaperByPaperIds(paperIds));
    }


    @GetMapping(value = "/paper/{paperId}")
    public AjaxResult getPaperTrack(@PathVariable("paperId") Long paperId)
    {
        return success(researchPaperService.selectResearchPaperTrackList(paperId));
    }
}
